//file:ServerCore\PacketAnalyzer.cpp


#include "PacketAnalyzer.h"
#include <windows.h>
#include "..\TransportLib\ProtocolLib.h"

CKPacketAnalyzer::CKPacketAnalyzer()
{
	const unsigned int dwMaxNum = 500;
	m_hSemaphore = ::CreateSemaphore(NULL, 0, dwMaxNum, NULL);
}


CKPacketAnalyzer::~CKPacketAnalyzer()
{
	::CloseHandle(m_hSemaphore);
	for(int index = 0; index < ANALYZER_THREAD_NUM; index++)
	{
		::CloseHandle(m_hThreads[index]);
	}
}



//************************************
// Method:    CKPacketAnalyzer::PostPacket
// Access:    public 
// Returns:   void
// Parameter: const CKPacket & packet
// Author:    kalrey
// Description: To post a packet to the buffer of analyzer
//************************************
inline void CKPacketAnalyzer::PostPacket(const CKPacket& packet )
{	
	m_PacketQueue.push(packet);
	::ReleaseSemaphore(m_hSemaphore, 1, NULL);
}



//************************************
// Method:    CKPacketAnalyzer::Analyzer
// Access:    private 
// Returns:   void
// Author:    kalrey
// Description: To analyze a packet that take out from buffer
//************************************
void CKPacketAnalyzer::Analyzer()
{
	while(1)
	{
		::WaitForSingleObject(m_hSemaphore, INFINITE);
		CKPacket packet = m_PacketQueue.front();
		switch (packet.m_ProtocolHeader.ProtocolFamily)
		{
		case PROTOCOL_RRPF:
			{

				break;
			}
		case PROTOCOL_CTPF:
			{
				break;
			}
		case PROTOCOL_HBPF:
			{
				break;
			}
		case PROTOCOL_DTPF:
			{
				break;
			}
		case PROTOCOL_CSTPF:
			{
				break;
			}	
		case PROTOCOL_CTRPF:
			{
				break;
			}
		}
	}
	
}


void CKPacketAnalyzer::BeginAnalyze()
{
	for(int index = 0; index < ANALYZER_THREAD_NUM; index++)
	{
		m_hThreads[index] = ::CreateThread(NULL, 0, ThreadAdapter, (void*)this, NULL, NULL);
	}
}


static DWORD CKPacketAnalyzer::ThreadAdapter(void *pParam)
{
	((CKPacketAnalyzer*)pParam)->Analyzer();
	return 0;
}